package bjtu.annotation.service;

import java.util.ArrayList;
import java.util.List;

import org.ansj.app.keyword.Keyword;
import org.ansj.app.summary.SummaryComputer;
import org.ansj.app.summary.pojo.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import bjtu.annotation.dao.LabelDao;
import bjtu.annotation.entity.Label;

@Service
@Transactional
public class LabelServiceImpl implements LabelService {
	@Autowired
	LabelDao ld;

	public void addLabels(List<Label> list) {
		// TODO Auto-generated method stub
		for (Label label : list) {
			ld.saveLabel(label);
		}
	}

	/**
	 * 选出所有的类
	 */
	public List<Label> selectAllLabels() {
		// TODO Auto-generated method stub
		return ld.selectAllLabels();
	}

	/**
	 * 选出所有的父类
	 */
	public List<Label> selectAllParentLabels() {
		return ld.selectAllParentIds();
	}

	public List<Label> fetchChildLables(Integer id) {
		return ld.selectChildLables(id);
	}

	public List<Label> selectAllChildLabels() {
		return ld.selectAllChildLabels();
	}

	// 关键词提取
	@Override
	public List<String> getKkeyWords(String title, String News, int keywordsNumber) throws Exception {
		// 复旦分词器
		/*
		 * ArrayList<String> keywords=new ArrayList<String>(); StopWords sw= new
		 * StopWords("models/stopwords"); CWSTagger seg = new
		 * CWSTagger("models/seg.m"); AbstractExtractor key = new
		 * WordExtract(seg,sw);
		 * 
		 * //you need to set the keywords number, here you will get 10 keywords
		 * Map<String,Integer> ans = key.extract(News, keywordsNumber);
		 * 
		 * for (Map.Entry<String, Integer> entry : ans.entrySet()) { String
		 * keymap = entry.getKey().toString(); String value =
		 * entry.getValue().toString(); keywords.add(keymap); }
		 */

		SummaryComputer summaryComputer = new SummaryComputer(300, title, News);
		Summary summary = summaryComputer.toSummary();
		List<Keyword> keywords = summary.getKeyWords();
		List<String> key = new ArrayList<String>();
		for(int i = 0; i < keywordsNumber && i < keywords.size(); i++){
			key.add(keywords.get(i).getName());
		}
		return key;
	}

}
